<?php
session_start();

function success(){
    $result=[
        'error'=>'0',
        'message'=>'成功'
    ];
    return json_encode($result);
}

function error($msg){
    $data=[
        'error'=>'1',
        'message'=>$msg
    ];
    return json_encode($data);
}
$username=$_POST['username'];
$password=$_POST['password'];
$config = include '../config/db.php';
$db = new PDO($config['dsn'],$config['username'],$config['password']);
$statement=$db->prepare('select * from user where username=:username');
$statement->execute([':username'=>$username]);
$user=$statement->fetch();
if(empty($username)){
    die(error('用户不能为空！'));
}

if(empty($password)){
    die(error('密码不能为空！'));
}

if(empty($user)){
    die(error('用户名不存在！'));
}

if($password!=$user['password']){
    die(error('密码不正确！'));
}
$_SESSION['user']=$username;
echo success();